#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<map>

using namespace std;

int totalFruit(vector<int>& fruits) {
    map<int, int> m;
    int l = 0, res = 0;
    for (int r = 0; r < fruits.size(); r++)
    {
        m[fruits[r]]++;
        while (m.size() > 2)
        {
            int t = fruits[l++];
            m[t]--;
            if (m[t] == 0)   m.erase(t);
        }
        res = max(res, r - l + 1);
    }
    return res;
}